package cxf.spring.demo.storage;

import cxf.spring.demo.domain.message.Message;
import cxf.spring.demo.domain.user.User;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.util.Map;
import java.util.TreeMap;

/**
 * User: Vlad Vinichenko (akerigan@gmail.com)
 * Date: 07.03.2009
 * Time: 16:26:53
 */
public class ClientStorage {

    File basePath;
    Map<Integer, Message> incomingMessages = new TreeMap<Integer, Message>();
    Map<Integer, Message> sentMessages = new TreeMap<Integer, Message>();
    User user;
    int messageId;

    public void setBasePath(String basePath) throws Exception {
        this.basePath = new File(basePath);
        if (!this.basePath.exists()) {
            boolean created = this.basePath.mkdirs();
            if (!created) {
                this.basePath = null;
            }
        } else {
            File userFile = new File(basePath, "user.xml");
            if (userFile.exists()) {
                JAXBContext jaxbContext = JAXBContext.newInstance(User.class);
                Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
                user = (User) unmarshaller.unmarshal(userFile);
            }
            File sentDir = new File(basePath, "sent");
            if (sentDir.exists()) {
                JAXBContext jaxbContext = JAXBContext.newInstance(Message.class);
                Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
                for (File file : sentDir.listFiles()) {
                    if (file.getName().endsWith(".xml")) {
                        Message message = (Message) unmarshaller.unmarshal(file);
                        sentMessages.put(message.getId(), message);
                        if (message.getId() > messageId) {
                            messageId = message.getId();
                        }
                    }
                }
            }
            File incomingDir = new File(basePath, "incoming");
            if (incomingDir.exists()) {
                JAXBContext jaxbContext = JAXBContext.newInstance(Message.class);
                Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
                for (File file : incomingDir.listFiles()) {
                    if (file.getName().endsWith(".xml")) {
                        Message message = (Message) unmarshaller.unmarshal(file);
                        incomingMessages.put(message.getId(), message);
                    }
                }
            }
        }
    }

    public Map<Integer, Message> getIncomingMessages() {
        return incomingMessages;
    }

    public void addIncomingMessage(Message incomingMessage) throws Exception {
        incomingMessages.put(incomingMessage.getId(), incomingMessage);
        if (basePath != null) {
            File incomingDir = new File(basePath, "incoming");
            boolean dirExists = true;
            if (!incomingDir.exists()) {
                dirExists = incomingDir.mkdir();
            }
            if (dirExists) {
                persistMessage(incomingMessage, incomingDir);
            }
        }
    }

    public Map<Integer, Message> getSentMessages() {
        return sentMessages;
    }

    public void addSentMessage(Message sentMessage) throws Exception {
        sentMessage.setId(messageId++);
        sentMessages.put(sentMessage.getId(), sentMessage);
        if (basePath != null) {
            File sentDir = new File(basePath, "sent");
            boolean dirExists = true;
            if (!sentDir.exists()) {
                dirExists = sentDir.mkdir();
            }
            if (dirExists) {
                persistMessage(sentMessage, sentDir);
            }
        }
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) throws Exception {
        this.user = user;
        if (basePath != null) {
            JAXBContext jaxbContext = JAXBContext.newInstance(User.class);
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.marshal(user, new File(basePath, "user.xml"));
        }
    }

    private void persistMessage(Message message, File dir) throws Exception {
        JAXBContext jaxbContext = JAXBContext.newInstance(Message.class);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.marshal(message, new File(dir, String.format("%d.xml", message.getId())));
    }

}

